package org.damazio.flight.checklists;

import org.damazio.flight.checklists.data.CheckListsSchema;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class CheckListsMain extends ListActivity {
  private static final String ITEM_ID_KEY = "item_id";
  private static final String ITEM_NAME_KEY = "item_name";
  private CheckListsSchema schema;
  private long parentId;
  private String parentName;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    schema = new CheckListsSchema(this);
    schema.openOrCreate();

    Bundle extras = getIntent().getExtras();

    parentId = 0;
    if (extras != null) {
      parentName = extras.getString(ITEM_NAME_KEY);
      if (parentName != null) {
        setTitle(parentName);
      }

      // Query for the proper lists
      parentId = extras.getLong(ITEM_ID_KEY, 0);
    }

    Cursor cursor = schema.findByParent(parentId);
    startManagingCursor(cursor);

    // TODO: Different icon depending on the type and status
    ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
        cursor, new String[] { CheckListsSchema.NAME_COLUMN }, new int[] { android.R.id.text1 });
    setListAdapter(adapter);
    getListView().setTextFilterEnabled(true);
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long itemId) {
    super.onListItemClick(l, v, position, itemId);

    Cursor cursor = (Cursor) getListAdapter().getItem(position);

    String itemType = cursor.getString(cursor.getColumnIndexOrThrow(CheckListsSchema.TYPE_COLUMN));
    String itemName = cursor.getString(cursor.getColumnIndexOrThrow(CheckListsSchema.NAME_COLUMN));
    if (itemType.equals(CheckListsSchema.ITEM_TYPE)) {
      // TODO: Open item
    } else if (itemType.equals(CheckListsSchema.LIST_TYPE)) {
      showCheckList(this, itemId, itemName, parentName);
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.add_list:
        addChecklist();
        break;
      case R.id.add_item:
        addItem();
        break;
      default:
        return false;
    }
    return true;
  }

  private void addItem() {
    if (parentId == 0) {
      // TODO: Instead disable the add item button if it's the top level
      // Don't allow top-level items
      Toast.makeText(this, R.string.no_top_level_items, Toast.LENGTH_LONG).show();
      return;
    }

    CheckListItemEdit.addNewItem(this, parentId);
  }

  private void addChecklist() {
    CheckListEdit.addNewList(this, parentId, parentName);
  }

  public static void showCheckList(Context context, long listId, String listName, String parentName) {
    if (parentName != null) {
      listName = parentName + ": " + listName;
    }

    Intent intent = new Intent(context, CheckListsMain.class);
    intent.putExtra(ITEM_ID_KEY, listId);
    intent.putExtra(ITEM_NAME_KEY, listName);
    context.startActivity(intent);
  }
}